package com.team.register;

import com.team.register.enums.PrintEnum;
import com.team.register.pojo.BaseUser;
import com.team.register.service.LoginService;
import com.team.register.service.LoginServiceImpl;
import com.team.register.service.SignUpService;
import com.team.register.service.SignUpServiceImpl;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @Program: register
 * @Description: 启动类
 * @author: Cooper
 * @date: 2021.10.15
 * @Version: 1.0
 */
public class Application {
    public static void main(String[] args) {
        start();
    }

    public static void start() {
        Scanner sc = new Scanner(System.in);
        System.out.println("******欢迎！******\r\n");
        System.out.println("1.登录\t2.注册\t3.退出\r\n");
        System.out.println("****************************");
        System.out.println("请输入你要操作的序号：");
        try {
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    signIn();
                    break;
                case 2:
                    boolean b = signUp();
                    if (b) {
                        signIn();
                        break;
                    }
                    break;
                case 3:
                    System.exit(0);
                default:
                    System.out.println("你输入的编号有误！");
            }
        } catch (InputMismatchException e) {
            System.out.println("请输入序号！");
        }
    }

    public static boolean signIn() {
        Scanner input = new Scanner(System.in);
        System.out.println("-------------请登录！------------");
        System.out.println("0.普通用户\t1.管理员\r\n");
        System.out.println("****************************");
        System.out.println("请输入你的登录角色：");
        String role = input.next();
        System.out.println("请输入你的用户名：");
        String username = input.next();
        System.out.println("请输入你的密码：");
        String password = input.next();
        LoginService loginService = new LoginServiceImpl();
        String[] strs = new String[]{role, username, password};
        boolean b = loginService.signIn(strs);
        if (b) {
            System.out.println(PrintEnum.LOGIN_SUCCESS.getMessage());
            return true;
        } else {
            System.out.println(PrintEnum.LOGIN_FAIL.getMessage());
            return false;
        }
    }

    public static boolean signUp() {
        Scanner sc = new Scanner(System.in);
        System.out.println("-------------请注册！------------");
        System.out.println("0.普通用户\t1.管理员\r\n");
        System.out.println("****************************");
        System.out.println("请输入你要创建的角色：");
        String role = sc.next();
        System.out.println("请输入你的用户名：");
        String username = sc.next();
        System.out.println("请输入你的密码：");
        String password = sc.next();
        SignUpService signUpService = new SignUpServiceImpl();
        String[] strs = new String[]{role, username, password};
        boolean b = signUpService.signUp(strs);
        if (b) {
            System.out.println(PrintEnum.REGISTER_SUCCESS.getMessage());
            return true;
        } else {
            System.out.println(PrintEnum.REGISTER_FAIL.getMessage());
            return false;
        }
    }
}
